import cairo, cStringIO
from pyglet import font, window, image
w, h = 400, 400
rot = 0.04
# pyglet
win = window.Window(width=w, height=h)
ft = font.load('Arial', 36)
text = font.Text(ft, 'Hello, World!')
# cairo
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
ctx = cairo.Context(surface)
ctx.translate(200, 200)

while not win.has_exit:
        # cairo
        ctx.rotate(rot)
        # clear to black
        ctx.set_source_rgb(0, 0, 0)
        ctx.paint()
        # lifted from pycairo example...
        ctx.set_line_width(15)
        ctx.set_source_rgb(255, 0, 0)
        ctx.move_to(0, -100)
        ctx.line_to(100, 100)
        ctx.rel_line_to(-200, 0)
        ctx.close_path()
        ctx.stroke()
        # output to file-like obj
        f = cStringIO.StringIO()
        surface.write_to_png(f)
        # rewind
        f.seek(0)
        # load
        pic = image.load('hint.png', file=f)
        #pyglet
        win.dispatch_events()
        win.clear()
        pic.blit(0, 0)
        text.draw()
        win.flip()
